<?php
/**
 * 不参加统计操作类
 *
 * @version 1.0
 * @date 2013-12-13
 */
class DetailController extends FController {
	public function accessRules() {
		$config = array('index' => 1);
		return ZRole::auth("nojoinrule_detail", $config);
	}
	public function __common() {
		parent::init();
		if (empty($this->cLoginUser)) {
			$this->redirect($this->createUrl('/site/login'));
		}
		$this->commonScript();
		Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/module_Sip.css');
	}
	//列表显示
	public function actionIndex() {
		$this->__common();
		$this->title = "不参加统计规则";
		$s['rowconfig'] = Nojoin::_getnojointuleconfig_row();
		$s['whereconfig'] =  Nojoin::_getnojointuleconfig_where(1);
		$sd['id'] = Yii::app()->request->getParam('id');
		$limit = 8;
		$sql = 'SELECT * FROM "{{nojoinrule_detail}}" WHERE "join_id"='.$sd['id'].' AND "is_del" = 0  ORDER BY "id" DESC';
		$count = 'SELECT COUNT("id") FROM "{{nojoinrule_detail}}" WHERE "is_del"= 0 AND "join_id"='.$sd['id'];
		$data = ZCommon::getDaoPageList($sql, $count, $limit);
		$pageUrl = $this->createUrl('/nojoin_rule/detail/index',$sd);
		$this->render('index',array(
		'data'	=>$data['listDatas'],
		'pages' => $data['pages'],
		'serch'	=> $s,
		'serchs'	=> $sd,
		'pageUrl' => $pageUrl
		));
	}
	//更新
	public function actionUpdate(){
		$this->layout = "//layouts/main_window";
		$id = Yii::app()->request->getparam('id');
		$data = Yii::app()->request->getparam('info');
		$date['rowconfig'] = Nojoin::_getnojointuleconfig_row();
		$date['whereconfig'] =  Nojoin::_getnojointuleconfig_where(1);
		$id = isset($id) ? $id : $data['id'];
		$model = NojoinruleDetail::model()->findByPk($id);
		if(empty($model)){
			$this->showmessage("数据出错,请返回重试");exit;
		}
		if(!empty($_POST['submit'])){
			
			$data['addwheres2'] = trim($data['addwheres2'],'AND ');
			$data['addwheres2'] = trim($data['addwheres2'],'OR ');
			$data['addwheres2'] = (!empty($data['addwheres2'])) ? $data['addwheres2'] : '';
			$data['addwheres2'] = preg_replace("~ *\"|\" *~","\"", $data['addwheres2']);
            $data['addwheres2'] = str_replace(array("AND","OR","IN","NOT"), array(" AND "," OR ", " IN ", " NOT "), $data['addwheres2']);
			
			$data['addwheres3'] = trim($data['addwheres3'],'AND ');
			$data['addwheres3'] = trim($data['addwheres3'],'OR ');
			$data['addwheres3'] = (!empty($data['addwheres3'])) ? $data['addwheres3'] : '';
			$data['addwheres3'] = preg_replace("~ *\"|\" *~","\"", $data['addwheres3']);
            $data['addwheres3'] = str_replace(array("AND","OR","IN","NOT"), array(" AND "," OR ", " IN ", " NOT "), $data['addwheres3']);
			if(empty($data['addwheres3']) || $data['addwheres3'] == 1){
				if(empty($data['addwheres2']) || $data['addwheres2'] == 1){
					$data['addwheres'] = '';
				}else{
					$data['addwheres'] = "(".$data['addwheres2'].")";
				}
			}else{
				if(empty($data['addwheres2']) || $data['addwheres2'] == 1){
					$data['addwheres'] = $data['addwheres3'];
				}else{
					$data['addwheres'] = $data['addwheres3']." AND (".$data['addwheres2'].")";
				}
			}
			
			$data['addwheres'] = str_replace("`",'"',$data['addwheres']);
			

			$data['status'] = (!empty($data['status'])) ? $data['status'] : 0;
		
			$model->range=$data['range'];
			$model->where=$data['where'];
			$model->status=$data['status'];
			$model->addwheres=$data['addwheres'];
			$model->remark=$data['remark'];
			$model->update_user = $this->cLoginUser['cuser']['id'];
			$model->update_time = time();
			if($model->save()){
				$join_id = $model->attributes;
				$this->redirect($this->createUrl('/nojoin_rule/detail/index',array('id'=>$join_id['join_id'])));
			}else{
				$this->showmessage("操作失败!");exit;
			}
		}else{
			$date['model'] = $model->attributes;
			
			$this->render('update',array('data'	=> $date));
		}
	}
	//添加
	public function actionAdd(){
		$this->layout = "//layouts/main_window";
		$data = Yii::app()->request->getparam('info');
		if(!empty($_POST['submit'])){
			$data['addwheres'] = trim($data['addwheres'],'AND');
			$data['addwheres'] = trim($data['addwheres'],'OR');
			$data['addwheres'] = (!empty($data['addwheres'])) ? $data['addwheres'] : '';
			$data['create_user'] = $this->cLoginUser['cuser']['id'];
			$data['create_time'] = time();
            $data['addwheres'] = preg_replace("~ *\"|\" *~","\"", $data['addwheres']);
			$data['range'] = preg_replace("~ *\"|\" *~","\"", $data['range']);
            $data['addwheres'] = str_replace(array("AND","OR","IN","NOT"), array(" AND "," OR ", " IN ", " NOT "), $data['addwheres']);
			
			$data['addwheres2'] = trim($data['addwheres2'],'AND');
			$data['addwheres2'] = trim($data['addwheres2'],'OR');
			$data['addwheres2'] = (!empty($data['addwheres2'])) ? $data['addwheres2'] : '';
			$data['addwheres2'] = preg_replace("~ *\"|\" *~","\"", $data['addwheres2']);
            $data['addwheres2'] = str_replace(array("AND","OR","IN","NOT"), array(" AND "," OR ", " IN ", " NOT "), $data['addwheres2']);
			if(empty($data['addwheres']) || $data['addwheres'] == 1){
				if(empty($data['addwheres2']) || $data['addwheres2'] == 1){
					$data['addwheres'] = '';
				}else{
					$data['addwheres'] = "(".$data['addwheres2'].")";
				}
			}else{
				if(empty($data['addwheres2']) || $data['addwheres2'] == 1){
					$data['addwheres'] = $data['addwheres'];
				}else{
					$data['addwheres'] = $data['addwheres']." AND (".$data['addwheres2'].")";
				}
			}

			$data['addwheres'] = str_replace("`",'"',$data['addwheres']);
            $data['is_del'] = 0;
			$nojoinruledetail = new NojoinruleDetail();
			$nojoinruledetail->attributes=$data; 
			if($nojoinruledetail->save()){
				$this->redirect($this->createUrl('/nojoin_rule/detail/index',array('id'=>$data['join_id'])));
			}else{
				$this->runShowMessge('index',"数据添加失败！");
			}
		}else{
			$this->redirect($this->createUrl('/nojoin_rule/detail/index',array('id'=>$data['join_id'])));
		}
	}
	//删除
	public function actionDelete(){
		$id = Yii::app()->request->getparam('id');
		if(!empty($id)){
			$model = NojoinruleDetail::model()->findByPk($id);
			if(empty($model)){
				$this->showmessage("您要删除的数据不存在!");exit;
			}
			$model->is_del=1;
			if ($model->save()) {
				$join_id = $model->attributes;
				$this->redirect($this->createUrl('/nojoin_rule/detail/index',array('id'=>$join_id['join_id'])));
			} else {
				$this->runShowMessge('index',"删除失败!");exit;
			}
		}else{
			$this->runShowMessge('index',"删除失败!");exit;
		}
	}
	//ajax获取不同类型的值
	public function actionajaxvalue(){

		$type = Yii::app()->request->getparam('type');
		$date = array();

		if (!empty($type)) {
			if($type=='school_name'){
				$schoolwhere = ' WHERE "is_del" = 0';
				$schools = Nojoin::_getschoolAll($schoolwhere);
				if (!empty($schools)){
					foreach ($schools as $k=>$v){
						$date[] = $v['school_name'];
					}
				}
			}else {
				$data = Nojoin::_getstudentrow($type);
				foreach ($data as $k=>$v){
					$date[] = $v[$type];
				}
			}
		}
		echo json_encode($date);
	}
}